﻿namespace TestWIP
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Linq;
    using System.Text;

    using com.crownpack.export;
    using com.crownpack.ExportConfigurationDao;
    using com.crownpack.Extensions.DoubleExtensions;
    using com.crownpack.Extensions.StringExtensions;
    using com.crownpack.Extensions.TimeSpanExtensions;
    using com.crownpack.SharePoint;

    using Microsoft.SharePoint;

    class Program
    {
        #region Methods

        /// <summary>
        /// Get a human readable string representation of the 
        /// amount of time the computer has been on since the OS started.
        /// </summary>
        /// <returns>How long the computer has been running in days, hours
        /// minutes and seconds.
        /// </returns>
        /// <remarks></remarks>
        public static string getUptime( )
        {
            String strResult = String.Empty;
            strResult += Convert.ToString( Environment.TickCount / 86400000 ) + " days, ";
            strResult += Convert.ToString( Environment.TickCount / 3600000 % 24 ) + " hours, ";
            strResult += Convert.ToString( Environment.TickCount / 120000 % 60 ) + " minutes, ";
            strResult += Convert.ToString( Environment.TickCount / 1000 % 60 ) + " seconds.";
            return strResult;
        }

        // Example usage:
        static void Main( string[ ] args )
        {
            test1( );
            Console.WriteLine( getUptime( ) );
            SharePointUri uri = new SharePointUri( "http://win-pv542842fga:8888/sites/cpwf/client/Lists/abc" );

            foreach ( Dictionary<string, string> s in read( ) )
            {
                foreach ( KeyValuePair<string, string> kvp in s )
                    Console.WriteLine( kvp.Key + " = " + kvp.Value );
            }
            double dbl = "$0".ToDouble( );
            TestTimeSpan( );
            Program pp = new Program( );
            pp.export( );
            Class2 b = new Class2( );
            b.AA( );
            return;
            b.c( );
            b.b( @"C:\abc.csv" );
            Class2 a = new Class2( );
            a.a( );
            return;
            Class1 o = new Class1( );
            o.Format( );
        }

        static IEnumerable read( )
        {
            string url = "http://win-pv542842fga:8888/sites/cpwf/export/2012-10-29_11.34.06_full.tsv";
            using ( SPSite spSite = new SPSite( url ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPFile spFile = spWeb.GetFile( url );
                    using ( StreamReader reader = new StreamReader( spFile.OpenBinaryStream( ) ) )
                    {
                        string[ ] h = new string[ 0 ];
                        while ( !reader.EndOfStream )
                        {
                            string contents = reader.ReadLine( );
                            if ( h.Length == 0 )
                                h = contents.Split( '\t' );
                            else
                            {
                                string[ ] v = contents.Split( '\t' );
                                Dictionary<string, string> d = new Dictionary<string, string>( );
                                for ( int i = 0; i < h.Length; i++ )
                                    d.Add( h[ i ], v[ i ] );
                                yield return d;
                            }
                        }
                    }
                }
            }
        }

        static void test1( )
        {
            string url = "http://win-pv542842fga:8888/sites/cpwf/client/Lists/abc";
            using ( SPSite spSite = new SPSite( url ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPList spList = spWeb.Lists["abcd"];
                    foreach ( SPListItem item in spList.Items )
                    {
                        SPFieldMultiLineText rt = item.Fields.GetField( "rt" ) as SPFieldMultiLineText;
                        string rts = rt.GetFieldValueAsText(item["rt"]);
                        var rtv = item[ "rt" ];
                        var rtx = rtv;
                        SPFieldMultiLineText mlt = item.Fields.GetField( "mlt" ) as SPFieldMultiLineText;
                        string mltt = rt.GetFieldValueAsText( item[ "mlt" ] );
                        var mltv = item[ "mlt" ];
                        var mltx = mltv;
                    }
                }
            }
        }

        static void TestTimeSpan( )
        {
            TimeSpan ts = new TimeSpan( 1, 3, 0, 3, 0 );
            Console.WriteLine( ts.ToString( ) );
            Console.WriteLine( ts.FormatAsTotalHoursMinutesSeconds( ) );
            Console.ReadLine( );
            return;
            //WL( String.Format( "{0:0}:{1:00}:{2:00}.{3:0}",
            //    ts.Hours, ts.Minutes, ts.Seconds,
            //    ts.Milliseconds / 10 ) );
            //WL( ts.Milliseconds );
            StringBuilder sb = new StringBuilder( );
            int abs = ts.TotalHours.ToInt32( );
            if ( abs > 0 )
            {
                string s = string.Empty;
                if ( abs <= 0 || abs > 1 )
                    s = "s";
                sb.AppendFormat( "{0} hour{1} ", abs, s );
            }
            //		if(ts.Minutes>0)
            {
                string s = string.Empty;
                if ( ts.Minutes <= 0 || ts.Minutes > 1 )
                    s = "s";
                sb.AppendFormat( "{0} minute{1} ", ts.Minutes, s );
            }
            //		string t=string.Empty;
            //		if(ts.Milliseconds>0)
            //		{
            //			t=string.Format(".{0}",ts.Milliseconds.ToString().TrimEnd('0'));
            //		}
            //if(ts.Seconds>0)
            {
                string s = string.Empty;
                if ( ts.Seconds <= 0 || ts.Seconds > 1 )
                    s = "s";

                sb.AppendFormat( "{0}.{2} second{1}", ts.Seconds, s, ts.Milliseconds.ToString( ).TrimEnd( '0' ) );
            }
            //		else if(ts.Milliseconds>0)
            //		{
            //			string s = "s";
            //			sb.AppendFormat("{0}{2} second{1}",ts.Seconds,s,t);
            //		}
            //		WL(sb.ToString().TrimEnd('0'));
            Console.WriteLine( sb.ToString( ) );
            Console.WriteLine( ts.ToString( ) );

            Console.ReadLine( );
        }

        void export( )
        {
            FullExportManager o = new FullExportManager( "http://win-pv542842fga:8888/sites/a_cp" );
            bool ok = o.Export( );
            bool a = ok;
        }

        #endregion Methods
    }
}